const _ = require('lodash');
const prefix = '/api/error';

module.exports = app => {
  const {router, model, middleware} = app;

  /** 读取错误 */
  router.get(prefix, middleware.checkLogin, async ctx => {
    const where = {resolve: false};
    if (ctx.query.all)
      delete where.resolve;
    ctx.body = await model.Error.findAll(
        {
          raw: true,
          where,
          order: [['updated_at', 'desc']],
        });
  });

  /** 清空错误记录 */
  router.get(prefix + '/clear', middleware.checkLogin, async ctx => {
    await model.Error.sync({force: true});
    ctx.body = 'ok';
  });
};

